//
//  MHShareCircleViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/27.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHShareCircleViewModel.h"
#import "MHCommonGroupViewModel.h"
#import "MHCommonArrowItemViewModel.h"

@implementation MHShareCircleViewModel
- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.circleInfo = params[MHViewModelUtilKey];
    }
    return self;
}


- (void)initialize{
    [super initialize];
    self.title = @"圈子设置";
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    MHCommonArrowItemViewModel *wechat = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"微信邀请" icon:@"icon_share_wechat"];
    __weak __typeof(self)weakSelf = self;
    wechat.operation = ^{
        if (weakSelf.qrCodeModel) {
            OSMessage *msg=[[OSMessage alloc] init];
            msg.title=self.qrCodeModel.title;
            msg.link = self.qrCodeModel.webUrl;
            msg.desc = self.qrCodeModel.intro;
            NSURL *url = [NSURL URLWithString:self.qrCodeModel.coverUrl];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *img = [[UIImage alloc] initWithData:data];
            UIImage *thumbImage;
            if (img) {
                thumbImage = img;
            }else
            {
                thumbImage = [UIImage imageNamed:@"AppIcon"];
            }
            NSData *compressData = [thumbImage compressWithLengthLimit:30 *1024];
            msg.image = compressData;
            [OpenShare shareToWeixinSession:msg Success:^(OSMessage *message) {
                
            } Fail:^(OSMessage *message, NSError *error) {
            }];
        }
    };
    
    MHCommonArrowItemViewModel *shareToTimeLine = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"发送到朋友圈" icon:@"icon_share_timeline"];
    
    shareToTimeLine.operation = ^{
        if (weakSelf.qrCodeModel) {
            OSMessage *msg=[[OSMessage alloc] init];
            msg.title= self.qrCodeModel.title;
            msg.link = self.qrCodeModel.webUrl;
            msg.desc = self.qrCodeModel.intro;
            NSURL *url = [NSURL URLWithString:self.qrCodeModel.coverUrl];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *img = [[UIImage alloc] initWithData:data];
            UIImage *thumbImage;
            if (img) {
                thumbImage = img;
            }else
            {
                 thumbImage = [UIImage imageNamed:@"AppIcon"];
            }
            NSData *compressData = [thumbImage compressWithLengthLimit:31 *1024];
            msg.image = compressData;
//            msg.image = UIImageJPEGRepresentation(thumbImage, 0.1);
            [OpenShare shareToWeixinTimeline:msg Success:^(OSMessage *message) {
                
            } Fail:^(OSMessage *message, NSError *error) {
            }];
        }
    };
    
    MHCommonArrowItemViewModel *link = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"创建邀请链接" icon:@"icon_share_link"];
    link.operation = ^{
         UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
         [pasteboard setString:weakSelf.qrCodeModel.webUrl];
        [MBProgressHUD mh_showTips:@"链接复制成功"];
    };
    MHCommonArrowItemViewModel *circleQrCode = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"圈子二维码" icon:@"icon_share_qrcode"];
    circleQrCode.model = self.circleInfo;
    group0.itemViewModels = @[ wechat,shareToTimeLine,link,circleQrCode];
    self.dataSource = @[group0];
    
    @weakify(self);
    self.fetchQrCodeLinkCommond = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[self.services.client fetchCircleQrCodeWith:self.circleInfo.autoId] mh_parsedResults];
    }];
    
}
@end
